import javax.swing.JPopupMenu;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.ButtonGroup;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * GVertexPopup is the popup menu for the vertices.
 *
 * @version 1.0
 * @author P.Jaya Krishna
 */
class GVertexPopup extends JPopupMenu implements ActionListener
{
	JMenu vshape;
	JRadioButtonMenuItem circle;
	JRadioButtonMenuItem rectangle;
	JMenuItem clearAllEdges;
	JMenuItem delete;
	JMenuItem properties;
	ButtonGroup shapegroup;
	
	GPanel gPanel;
	VertexAttr currentVertex;
	
	/**
	 * Creates a popup menu for the specified GPanel "gPanel" at (x,y).
	 */
	public GVertexPopup(GPanel gPanel, int x, int y)
	{
		this.gPanel = gPanel;
		try
		{
			currentVertex = gPanel.getGraph().getVertex(gPanel.getCurrVertex());
		}
		catch(IllegalVertexException e)
		{
			// Not a problem even if failed here.
		}
		
		vshape 			= new JMenu("Shape");
		vshape.setMnemonic('S');
		circle 			= new JRadioButtonMenuItem("Circle");
		circle.setMnemonic('C');
		rectangle 		= new JRadioButtonMenuItem("Rectangle");
		rectangle.setMnemonic('R');
		shapegroup  	= new ButtonGroup();
		clearAllEdges 	= new JMenuItem("Clear All Edges", 'C');
		delete			= new JMenuItem("Delete Vertex", 'D');
		properties		= new JMenuItem("Properties", 'P');
		
		if(currentVertex.getShape() == VertexAttr.SHAPE_CIRCLE)
			circle.setSelected(true);
		else
			rectangle.setSelected(true);
		
		add(vshape);
		vshape.add(circle);
		vshape.add(rectangle);
		shapegroup.add(circle);
		shapegroup.add(rectangle);
		addSeparator();
		add(clearAllEdges);
		add(delete);
		addSeparator();
		add(properties);
		
		circle.addActionListener(this);
		rectangle.addActionListener(this);
		clearAllEdges.addActionListener(this);
		delete.addActionListener(this);
		properties.addActionListener(this);
		
		setLocation(x, y);
		show(gPanel, x, y);
	}
	
	public void actionPerformed(ActionEvent ae)
	{
		String s = (String) ae.getActionCommand();
		
		if(s.equals("Circle"))
		{
			if(currentVertex.getShape() != VertexAttr.SHAPE_CIRCLE)
			{
				currentVertex.setShape(VertexAttr.SHAPE_CIRCLE);
				try
				{
					gPanel.addUndo();
					gPanel.getGraph().setVertex(gPanel.getCurrVertex(), currentVertex);
				}	catch(IllegalVertexException  e) {	}
			}
		}
		else if(s.equals("Rectangle"))
		{
			if(currentVertex.getShape() != VertexAttr.SHAPE_RECTANGLE)
			{
				currentVertex.setShape(VertexAttr.SHAPE_RECTANGLE);
				try
				{
					gPanel.addUndo();
					gPanel.getGraph().setVertex(gPanel.getCurrVertex(), currentVertex);
				}	catch(IllegalVertexException  e) {	}
			}
		}
		else if(s.equals("Clear All Edges"))
		{
			try
			{
				gPanel.addUndo();
				gPanel.getGraph().removeAllEdges(gPanel.getCurrVertex());
			}
			catch(IllegalVertexException e)
			{
				// Not a problem even if failed here.
			}
		}
		else if(s.equals("Delete Vertex"))
		{
			gPanel.doRemoveVertex();
		}
		else if(s.equals("Properties"))
		{
			new GVertexDialog(gPanel, currentVertex);
			
			try
			{
				gPanel.addUndo();
				gPanel.getGraph().setVertex(gPanel.getCurrVertex(), currentVertex);
			}
			catch(IllegalVertexException e)
			{
				// Not a problem even if failed here.
			}
		}
		gPanel.repaint();
	}
}